//
//  BAMainListVC.m
//  YunFu
//
//  Created by Bager on 2018/9/4.
//  Copyright © 2018年 yunfu. All rights reserved.
//

#import "BAMainListVC.h"

#import "CyclePagerView.h"
#import "MMComboBox.h"
#import "BAShopCell.h"
#import "BADetailVC.h"

@interface BAMainListVC ()<UITableViewDelegate,UITableViewDataSource,MMComBoBoxViewDataSource, MMComBoBoxViewDelegate>
{
    UITableView *table;
}
@property (nonatomic, strong) NSMutableArray *mutableArray;
@property (nonatomic, strong) MMComBoBoxView *comBoBoxView;

@end

@implementation BAMainListVC

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.comBoBoxView dimissPopView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"美食";
    
    [self createTable];
}

/**
 *  创建table
 */
- (void)createTable
{
    table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kPublicNavHeight)];
    table.delegate = self;
    table.dataSource = self;
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
//        [table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:YES];
//    });
    [self.view addSubview:table];
    
    UIView *noLine = [[UIView alloc] initWithFrame:CGRectZero];
    table.tableFooterView = noLine;
}

#pragma mark - table协议

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return 130;
    }
    else
    {
        return 100;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 1;
    }
    else
    {
        return 30;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 0.01;
    }
    else
    {
        return 44;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return [UIView new];
    }
    UIView *headBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
    headBg.backgroundColor = [UIColor whiteColor];
    
    [headBg addSubview:self.comBoBoxView];
    
    return headBg;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellIdentifier"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        CyclePagerView *page = [[CyclePagerView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 130) imgArray:@[@"",@"",@""]];
        [table addSubview:page];
        
        DefineWeakSelf;
        page.callBack = ^(id result) {
            
        };
        return cell;
    }
    else
    {
        BAShopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifiers"];
        if (!cell)
        {
            cell = [[BAShopCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellIdentifiers"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1)
    {
        BADetailVC *detail = [[BADetailVC alloc] init];;
        [self.navigationController pushViewController:detail animated:YES];
    }
}

- (MMComBoBoxView *)comBoBoxView
{
    if (!_comBoBoxView)
    {
        _comBoBoxView = [[MMComBoBoxView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
        _comBoBoxView.backgroundColor = [UIColor whiteColor];
        _comBoBoxView.dataSource = self;
        _comBoBoxView.delegate = self;
        [_comBoBoxView reload];
        _comBoBoxView.showBlock = ^{
            [self.view addSubview:self.comBoBoxView];
        };
        _comBoBoxView.dismissBlock = ^{
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [table reloadData];
            });
        };
    }
    return _comBoBoxView;
}

#pragma mark - MMComBoBoxViewDataSource
- (NSUInteger)numberOfColumnsIncomBoBoxView :(MMComBoBoxView *)comBoBoxView
{
    return self.mutableArray.count;
}

- (MMItem *)comBoBoxView:(MMComBoBoxView *)comBoBoxView infomationForColumn:(NSUInteger)column
{
    return self.mutableArray[column];
}

#pragma mark - MMComBoBoxViewDelegate
- (void)comBoBoxView:(MMComBoBoxView *)comBoBoxViewd didSelectedItemsPackagingInArray:(NSArray *)array atIndex:(NSUInteger)index
{
    MMItem *rootItem = self.mutableArray[index];
    switch (rootItem.displayType)
    {
        case MMPopupViewDisplayTypeNormal:
        case MMPopupViewDisplayTypeMultilayer:
        {
            NSMutableString *title = [NSMutableString string];
            __block NSInteger firstPath;
            [array enumerateObjectsUsingBlock:^(MMSelectedPath * path, NSUInteger idx, BOOL * _Nonnull stop) {
                [title appendString:idx?[NSString stringWithFormat:@";%@",[rootItem findTitleBySelectedPath:path]]:[rootItem findTitleBySelectedPath:path]];
                if (idx == 0)
                {
                    firstPath = path.firstPath;
                }
            }];
            NSLog(@"当title为%@时，所选字段为 %@",rootItem.title ,title);
            break;
        }
        case MMPopupViewDisplayTypeFilters:
        {
            break;
        }
        default:
            break;
    }
}

#pragma mark - Getter
- (NSMutableArray *)mutableArray
{
    if (!_mutableArray)
    {
        _mutableArray = [NSMutableArray array];
        
        MMSingleItem *rootItem1 = [MMSingleItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:@"全部分类"];
        [rootItem1 addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected isSelected:YES titleName:@"全部分类" subtitleName:nil code:nil]];
        [rootItem1 addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:[NSString stringWithFormat:@"火锅"]]];
        [rootItem1 addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:[NSString stringWithFormat:@"日韩料理"]]];
        [rootItem1 addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:[NSString stringWithFormat:@"西餐"]]];
        [rootItem1 addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:[NSString stringWithFormat:@"烧烤"]]];
        
        MMSingleItem *rootItem2 = [MMSingleItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:@"附近"];
        [rootItem2 addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected isSelected:YES titleName:@"附近" subtitleName:nil code:nil]];
        [rootItem2 addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:[NSString stringWithFormat:@"欧亚"]]];
        [rootItem2 addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:[NSString stringWithFormat:@"河南市场"]]];
        [rootItem2 addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:[NSString stringWithFormat:@"现代百货"]]];
        [rootItem2 addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:[NSString stringWithFormat:@"影剧院"]]];
        
        MMSingleItem *rootItem3 = [MMSingleItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:@"智能排序"];
        [rootItem3 addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:[NSString stringWithFormat:@"智能排序"]]];
        [rootItem3 addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:[NSString stringWithFormat:@"离我最近"]]];
        [rootItem3 addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:[NSString stringWithFormat:@"好评优先"]]];
        [rootItem3 addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:[NSString stringWithFormat:@"人气最高"]]];
        
        MMSingleItem *rootItem4 = [MMSingleItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:@"筛选"];
        [rootItem4 addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected isSelected:YES titleName:@"筛选" subtitleName:nil code:nil]];
        [rootItem4 addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:[NSString stringWithFormat:@"免预约"]]];
        [rootItem4 addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:[NSString stringWithFormat:@"外卖送餐"]]];
        [rootItem4 addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:[NSString stringWithFormat:@"新店"]]];
        [rootItem4 addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:[NSString stringWithFormat:@"在线排队"]]];
        
        [_mutableArray addObject:rootItem1];
        [_mutableArray addObject:rootItem2];
        [_mutableArray addObject:rootItem3];
        [_mutableArray addObject:rootItem4];
    }
    return _mutableArray;
}



@end
